﻿--[[
    @Copyright 2015, Tencent Inc. All rights reserved.
    @brief    hardware detect
    @autor    stardradyin   
    @version  1.0
    @date     2017-04-08
]]--

hardware_dectect_control =
{
    default_callback_func = "cj_hardware_check_result";

    default_toolbox_check_callback_func = "hc_hardware_check_result";
    default_gamelist_callback_func = "hc_update_hardware_game_list";

    --初始化函数
    init = function()
        base_utility.log("hardware_dectect_control.init called");

        ierd_tgp.tpf_ui.Require("tool_box\\model.lua");
        ierd_tgp.tpf_ui.Require("tool_box\\control.lua");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");

        -- Add listener
        if tpfui ~= nil then
            tpfui:AddLuaListener(ierd_tgp.accelerate_module.ACCE_ON_HARDWARE_CHECK_END, "hardware_dectect_control.on_hardware_check_end");
            tpfui:RegisterJsEvent("request_hardware_check_browser", "hardware_dectect_control.request_hardware_check_browser");
        end;
        event_center.add_listener(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_HC, "hardware_dectect_control.on_js_callback");
    end;


    --接收消息from JS
    on_js_callback = function(event_id, jsonp)
        base_utility.log("hardware_dectect_control.on_js_callback called");
        local data_table = base_utility.json_to_table(jsonp);
        if data_table == nil then
            return;
        end;

        local op_name = data_table["fuc_name"];
        local json = data_table["json"];
        local json_table = base_utility.json_to_table(json);
        if op_name == nil then
            return;
        end;

        base_utility.log("hardware_dectect_control.on_js_callback op_name="..op_name);

        if op_name == "jc_hc_request_check" then
            hardware_dectect_control.request_hardware_check(json_table);
        elseif op_name == "hc_get_game_list" then
            hardware_dectect_control.get_hardware_game_list(json_table);
        elseif op_name == "hc_request_check" then
            hardware_dectect_control.request_hardware_check(json_table);
        end;
    end;

    get_hardware_game_list = function(param_table)
        if tool_box_model.game_base_info_ready == false then
            tool_box_control.init_game_base_info();
        end;

        local game_list, hold = hardware_dectect_control.get_game_list(main_type, category, sub_type, sort);
        local game_list_info = hardware_dectect_control.generate_game_info(game_list);

        hardware_dectect_control.lua_call_js(hardware_dectect_control.default_gamelist_callback_func,
            base_utility.table_to_json(game_list_info), CID_TOOLBOX_QBWEB);
    end;

    get_game_list = function(main_type, category, sub_type, sort)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local game_category = ierd_tgp.game_library.GameCategory:new_local();
        gl_adapter:get_game_category(main_type, category, sub_type, sort, game_category);
        base_utility.log("[hardware_dectect_control] get_game_list "..game_category.game_count);
        return game_category;
    end;

    generate_game_info = function(game_list)
        game_list_info = {};
        
        local i = 0;
        local j = 0;
        for i = 0, game_list.game_count - 1, 1 do
            local game_id = game_list.game_list[i];
            local game_detail_info = hardware_dectect_control.get_game_base_info(game_id);
            if game_detail_info ~= nil and game_detail_info.type ~= game_library_model.MOBILE_GAME_TYPE then
                local game_info = {};
                game_info.game_id = game_id;
                game_info.game_icon = game_detail_info.poster_url;
                game_info.game_name = game_detail_info.name;
                game_info.e_name = game_detail_info.e_name;
                game_info.game_main_type = game_detail_info.main_type;
                game_info.game_type = game_detail_info.type;
                game_info.game_sub_type = game_detail_info.sub_type;
                game_info.distribution_channels = game_detail_info.distribution_channels;
                game_info.game_desc = game_detail_info.slogan;
                game_info.game_new = game_detail_info.is_new;
                game_info.alpha = game_detail_info.name_first_letter;
                game_info.name_type = game_detail_info.name_type;
                game_info.game_installed = game_detail_info.is_installed;
                game_info.game_tags = game_detail_info.game_tags;
                game_info.tags = game_detail_info.tags;
                game_info.comments = game_detail_info.comments;
                game_info.icon_url = game_detail_info.banner_icon_url;
                game_info.release_date = game_detail_info.release_date;
                game_info.banner_url = game_detail_info.banner_icon_url;
                game_info.popularity = game_detail_info.popularity;

                game_list_info[j] = game_info;
                j = j + 1;
            end
        end
        
        return game_list_info;
    end;

    get_game_base_info = function(game_id)
        local i = 0;
        for i = 0, tool_box_model.all_game_info.count - 1, 1 do
            if game_id == tool_box_model.all_game_info.game_base_info[i].id then
                return tool_box_model.all_game_info.game_base_info[i];
            end
        end
    end;

    request_hardware_check_browser = function (func_name, json, browser_id)
        base_utility.log(string.format(
                "hardware_dectect_control.request_hardware_check_browser called, json:%s", json));

        if func_name ~= "request_hardware_check_browser" then
            return;
        end;

        local param = base_utility.json_to_table(json);
        if param == nil or param.data == nil then
            base_utility.log("hardware_dectect_control.request_hardware_check_browser param invalid");
            return;
        end;

        local request_data = param.data;

        if request_data.game_id == nil then
            base_utility.log("hardware_dectect_control.request_hardware_check_browser param invalid");
            return;
        end;

        local game_id = 0;
        if type(request_data.game_id) == 'string' then
            game_id = tonumber(request_data.game_id);
        elseif type(request_data.game_id) == 'number' then
            game_id = request_data.game_id;
        end;

        local force_request = false;

        if request_data.force_request ~= nil then
            if request_data.force_request == 1 or request_data.force_request == "1" then
                base_utility.log("hardware_dectect_control.request_hardware_check game_id, force request = true");
                force_request = true;
            end;
        end;

        local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
        rsp_table.request_browser_id = browser_id;
        rsp_table.request_param = param;

        if tool_box_model.hardware_check_browser_requests[game_id] ~= nil then
            local next_count = tool_box_model.hardware_check_browser_requests[game_id].count;
            tool_box_model.hardware_check_browser_requests[game_id][next_count] = rsp_table;
            tool_box_model.hardware_check_browser_requests[game_id].count
                = tool_box_model.hardware_check_browser_requests[game_id].count + 1;
        else
            local cur_list = {};
            cur_list.count = 1;
            cur_list[0]  = rsp_table;
            tool_box_model.hardware_check_browser_requests[game_id] = cur_list;

            local accModule =
            base_utility.get_component_adapter("IAccelerate_module",
                "ierd_tgp.accelerate_module.GetAcceModule();");
            if accModule == nil then
                base_utility.log("hardware_dectect_control.request_hardware_check_browser acce null");
                return;
            end;

            accModule:request_hardware_check(game_id, force_request);
        end;

        base_utility.log("hardware_dectect_control.request_hardware_check_browser add new requst for game="
            ..game_id..', now ['..tool_box_model.hardware_check_browser_requests[game_id].count..'] wait for callback');

        
    end;

    request_hardware_check = function (param_table)
        base_utility.log("hardware_dectect_control.request_hardware_check called");

        if param_table == nil then
            return;
        end;

        local json_table = nil;
        if param_table.request_src == "toolbox" then
            json_table = param_table;
        elseif param_table.request_src == "mainframe" then
            json_table = param_table;
        end;

        if json_table == nil then
            return;
        end;

        if json_table.game_id == nil then
            return;
        end;

        local force_request = false;

        if json_table.force_request ~= nil then
            if json_table.force_request == 1 or json_table.force_request == "1" then
                base_utility.log("hardware_dectect_control.request_hardware_check game_id, force request = true");
                force_request = true;
            end;
        end;

        base_utility.log("hardware_dectect_control.request_hardware_check game_id="
            ..json_table.game_id..', src='..json_table.request_src);

        local accModule =
            base_utility.get_component_adapter("IAccelerate_module",
                "ierd_tgp.accelerate_module.GetAcceModule();");
        if accModule == nil then
            base_utility.log("hardware_dectect_control.request_hardware_check acce null");
            return;
        end;

        local cur_game_id = 0;
        if type(json_table.game_id) == 'string' then
            cur_game_id = tonumber(json_table.game_id);
        elseif type(json_table.game_id) == 'number' then
            cur_game_id = json_table.game_id;
        end;
        
        accModule:request_hardware_check(cur_game_id, force_request);
    end;

    on_hardware_check_end = function(event_id, jsonp)
        base_utility.log("hardware_dectect_control.on_hardware_check_end called");
        local data_table = base_utility.json_to_table(jsonp);
        if data_table == nil then
            return;
        end;

        if data_table.data == nil or data_table.game_id == nil then
            return;
        end;

        local cur_game_id = 0;
        if type(data_table.game_id) == 'string' then
            cur_game_id = tonumber(data_table.game_id);
        elseif type(data_table.game_id) == 'number' then
            cur_game_id = data_table.game_id;
        end;

        base_utility.log("hardware_dectect_control.on_hardware_check_end, game_id="..cur_game_id);
        local qbweb_id = 0;

        base_utility.log("hardware_dectect_control.on_hardware_check_end, send to toolbox");
        local callback_func_name = hardware_dectect_control.default_toolbox_check_callback_func;
        qbweb_id = CID_TOOLBOX_QBWEB;
        hardware_dectect_control.lua_call_js(callback_func_name, jsonp, qbweb_id);

        base_utility.log("hardware_dectect_control.on_hardware_check_end, send to mainframe");
        callback_func_name = hardware_dectect_control.default_callback_func;
        qbweb_id = CID_MAINFRAME_QBWEB;
        hardware_dectect_control.lua_call_js(callback_func_name, jsonp, qbweb_id);

        -- browser request
        if tool_box_model.hardware_check_browser_requests[cur_game_id] ~= nil then
            local all_count = tool_box_model.hardware_check_browser_requests[cur_game_id].count;
            if type(all_count) ~= 'number' then
                return;
            end;

            for idx = 0, all_count - 1 do
                local cur_rsp_table = tool_box_model.hardware_check_browser_requests[cur_game_id][idx];
                cur_rsp_table.data = data_table;
                if cur_rsp_table.request_param.callback_func ~= nil and cur_rsp_table.request_param.callback_func ~= "" then
                    local rsp_json = base_utility.table_to_json(cur_rsp_table);
                    web_utility.lua_call_js_by_id_ex(cur_rsp_table.request_param.callback_func,
                        rsp_json, cur_rsp_table.request_browser_id);
                end;
            end;
            tool_box_model.hardware_check_browser_requests[cur_game_id] = nil;
            base_utility.log("hardware_dectect_control.on_hardware_check_end, send to request browser");
        end;
    end;


    lua_call_js = function (name, param, custom_id)
        ierd_tgp.tpf_ui.Log("hardware_dectect_control.on_hardware_check_end lua_call_JS start, name="..name);
        web_utility.lua_call_js(name, param, custom_id);
        ierd_tgp.tpf_ui.Log("hardware_dectect_control.on_hardware_check_end lua_call_JS success, name="..name);
    end;
};

hardware_dectect_control.init();